/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plechinger.mcremote.server.strategy;

import com.plechinger.mcremote.data.*;
import com.plechinger.mcremote.server.craft.CraftLogElement;
import com.plechinger.mcremote.server.craft.CraftServer;
import java.util.List;

/**
 *
 * @author Lukas
 */
@Strategy(method = RequestMethod.STATUS)
public class StatusStrategy implements ServerStrategy {

    public Response process(Request request) {
        Response response = new Response();
        try {
            int countWarnings = 0;
            int countErrors = 0;
            try {
                List<CraftLogElement> log = CraftServer.getServer().getLog();

                for (CraftLogElement craftLogElement : log) {
                    if (craftLogElement.getType() == CraftLogElement.Type.WARNING) {
                        countWarnings++;
                    } else if (craftLogElement.getType() == CraftLogElement.Type.ERROR) {
                        countWarnings++;
                    }
                }
            } catch (Exception e) {
            }
            int countplayers = 0;
            try {
                countplayers = CraftServer.getServer().playerList().size();
            } catch (Exception e) {
            }
            long time = 0;
            try {
                time = CraftServer.getServer().getRuntime();
            } catch (Exception e) {
            }
            Status status = new Status(CraftServer.getServer().isStarted(), countWarnings, countErrors, countplayers, time);

            response.setResponseObject(status);
            response.setStatus(ResponseStatus.OK);
            response.setMessage("Status");
        } catch (Exception ex) {
            response.setStatus(ResponseStatus.ERROR);
            response.setMessage(ex.getLocalizedMessage());
            response.setResponseObject(ex);
        }

        return response;

    }
}